var pay = {

    load: function(){
        $("#flex").flexReload();
    },

    edit: {

        open: function( id ) {

            if (!id) id = 0;

            E.popup.open({
                id: "pop1",
                title: id == 0 ? '新增支付方式' : '修改支付方式',
                css: "width: 800px;",
                btnName: "保存",
                btnCall: "pay.edit.check();"
            });

            if (id > 0) {

                E.ajax_get({
                    url: '/invokingAjax-backend/system/payType.ajax?operFlg=2',
                    data: {
                        id: id
                    },
                    call: function( o ) {

                        if (o.code == 200) {
                            $('#payTypeID').val(o.data.payTypeID);
                            $('#payTypeName').val(o.data.payTypeName);
                            if (o.data.logo_img) {
                                $('#logo_img_box').html('<img src="' + o.data.logo_img + '"> <a href="javascript: void(0);" id="del_log_img">删除</a>').show();
                            }
                            $('#logo_img').val(o.data.logo_img);
                            $('#memo').val(o.data.memo);
                            $('#type').val(o.data.type);
                            $('#payment_platform-' + o.data.payment_platform).iCheck('check');
                            $('#web_usable-' + o.data.web_usable).iCheck('check');
                            $('#wap_usable-' + o.data.wap_usable).iCheck('check');
                            $('#wx_usable-' + o.data.wx_usable).iCheck('check');
                            $('#app_usable-' + o.data.app_usable).iCheck('check');
                            $('#backend_usable-' + o.data.backend_usable).iCheck('check');
                        } else {
                            E.popup.close();
                            E.alert(o.message);
                        }

                    }
                });

            }

            $('.square-radio').iCheck({
                checkboxClass: 'icheckbox_square-blue',
                radioClass: 'iradio_square-blue',
                increaseArea: '20%' // optional
            });

        },

        check: function() {

            this.dt = E.getFormValues('pay_add_form');
            var error_array = new Array();

            if (E.isEmpty(this.dt.payTypeName)) {
                error_array[0] = '支付方式名称不能为空';
            }
            if (this.dt.type == 0) {
                error_array[1] = '请选择支付方式类型';
            }

            if (error_array.length > 0) {
                E.alert(error_array.join('<br>'));
            } else {
                this.dt.logo_img_box = $('#logo_img_box').html();
                E.loadding.open('正在保存支付方式，请稍候...');
                E.ajax_post({
                    url: '/invokingAjax-backend/system/payType.ajax?operFlg=100&act=edit',
                    data: this.dt,
                    call: "pay.edit.result"
                });
            }

        },

        result: function( o ) {

            E.loadding.close();

            if (o.code == 200) {
                E.popup.close();
                E.alert(o.message, 2, 'pay.load');
            } else {
                E.alert(o.message);
            }

        }

    },

    del: {

        p_array: '',

        check: function() {

            var p_obj = $('#flex').find('tr.trSelected');
            if (p_obj.length == 0) {
                E.alert('请选择要删除的支付方式');
            } else {

                var temp_array = new Array();
                var error_flg = 0;

                $.each(p_obj, function(k, v) {
                    var id = $(v).attr('id').replace('row', '');
                    if (id == 1 || id == 2 || id == 3) {
                        error_flg = 1;
                        return false;
                    }
                    temp_array.push($(v).attr('id').replace('row', ''));
                });

                if (error_flg == 1) {
                    E.alert('支付方式ID[1,2,3]是系统默认的支付方式<br />不可以删除');
                } else {
                    this.p_array = temp_array;
                    E.confirm('您确认要删除选中的支付方式吗？', 'pay.del.execute');
                }

            }

        },

        execute: function() {
            E.loadding.open('正在删除支付方式，请稍候...');
            E.ajax_post({
                url: '/invokingAjax-backend/system/payType.ajax?operFlg=100&act=del',
                data: {
                    p_array: this.p_array
                },
                call: 'pay.del.result'
            });
        },

        result: function( o ) {

            E.loadding.close();
            if (o.code == 200) {
                E.alert('删除成功', 2, 'pay.load');
            } else {
                E.alert(o.message);
            }

        }

    },

    change: {

        open: function( payTypeID, useFlg ) {

            this.payTypeID = payTypeID;
            this.useFlg = useFlg;

            if (useFlg == 1) {
                E.confirm('您确认要禁用该支付方式吗？', 'pay.change.execute')
            } else {
                E.confirm('您确认要启用该支付方式吗？', 'pay.change.execute')
            }

        },

        execute: function() {
            if (this.useFlg) {
                E.loadding.open('正在禁用支付方式，请稍候...');
            } else {
                E.loadding.open('正在启用支付方式，请稍候...');
            }
            E.ajax_get({
                url: '/invokingAjax-backend/system/payType.ajax?operFlg=100&act=change',
                data: {
                    payTypeID: this.payTypeID
                },
                call: 'pay.change.result'
            });
        },

        result: function( o ) {

            E.loadding.close();
            if (o.code == 200) {
                E.alert(o.message, 2, 'pay.load');
            } else {
                E.alert(o.message);
            }

        }

    }

};

//货到付款，付款方式
var paymentMethod = {

    payTypeID: '',

    list: {

        open: function( payTypeID ) {

            self.location = '/quanliadmin/app/system/payType.pscript?operFlg=2&payTypeID=' + payTypeID;

            //this.search();

        },

        search: function() {

            E.ajax_post({
                url: '/invokingAjax-backend/system/payType.ajax?operFlg=3',
                data: {
                    payTypeID: paymentMethod.payTypeID
                },
                call: 'paymentMethod.list.result'
            });

        },

        result: function( o ) {

            if (o.code == 200) {

                var html = '';

                $.each(o.data, function(k, v) {

                    if (edit_permission) {
                        html += '<tr id="payment_method_line_' + v.id +'">';
                        html += '<td>';
                        html += '<input class="form-control w150" type="text" id="paymentMethod_' + v.id +'" value="' + v.name +'" placeholder="请输入付款方式名称" />';
                        html += '</td>';


                        html += '<td>';
                        html += '<select class="form-control" name="type_id_' + v.id +'" id="type_id_' + v.id +'" style="width: 160px;">';
                        html += '<option value="">请选择付款方式类型</option>';
                        if(v.type == 1){
                            html += '<option value="1" selected>现金</option>';
                            html += '<option value="2" >刷卡</option>';
                        }
                        if(v.type == 2){
                            html += '<option value="1">现金</option>';
                            html += '<option value="2" selected>刷卡</option>';
                        }
                        html += '</select>';
                        html += '</td>';

                        html += '<td>';
                        html += '<button type="button" class="btn btn-primary" onclick="paymentMethod.edit.check(' + v.id +');">修改</button>';
                        html += '&nbsp;&nbsp;<button type="button" class="btn btn-primary" onclick="paymentMethod.del.check(' + v.id +');">删除</button>';
                        html += '</td>';
                        html += '</tr>';
                    } else {
                        html += '<tr>';
                        html += '<td>';
                        html += v.name;
                        html += '</td>';
                        html += '</tr>';
                    }

                });

                $('#payment_method_list').html(html);

            }

        }

    },

    edit: {

        check: function( id ) {

            this.id = id;

            var payment_method_name = E.trim($('#paymentMethod_' + id).val());

            var type = E.trim($('#type_id_' + id).val());

            var errorMsg = "";
            if (!payment_method_name) {
                errorMsg += "请输入付款方式名称<br>"
            }
            if(!type){
                errorMsg +="请选择付款方式类型"
            }
            if (errorMsg) {
                E.alert(errorMsg);
                return false;
            }else{
                E.loadding.open('正在保存，请稍候...');
                E.ajax_post({
                    url: '/invokingAjax-backend/system/payType.ajax?operFlg=100&act=m_edit',
                    data: {
                        payTypeID: paymentMethod.payTypeID,
                        id: id,
                        name: payment_method_name,
                        type:type
                    },
                    call: 'paymentMethod.edit.result'
                });
            }


        },

        result: function( o ) {

            E.loadding.close();
            if (o.code == 200) {
                if (this.id == 0) {
                    $('#paymentMethod_0').val('');
                    paymentMethod.list.search();
                }
                E.alert(o.message, 2);
            } else {
                E.alert(o.message);
            }

        }

    },

    del: {

        check: function(id) {

            this.id = id;
            E.confirm('您确认要删除该付款方式吗？', 'paymentMethod.del.execute');

        },

        execute: function() {

            E.loadding.open('正在删除，请稍候...');
            E.ajax_post({
                url: '/invokingAjax-backend/system/payType.ajax?operFlg=100&act=m_del',
                data: {
                    payTypeID: paymentMethod.payTypeID,
                    id: this.id
                },
                call: 'paymentMethod.del.result'
            });

        },

        result: function( o ) {

            E.loadding.close();
            if (o.code == 200) {
                E.alert(o.message, 2);
                $('#payment_method_line_' + this.id).remove();
            } else {
                E.alert(o.message);
            }

        }

    }

};


//在线支付参数设置
var online = {

    edit: {

        payTypeID: '',

        search: function() {

            E.ajax_post({
                url: '/invokingAjax-backend/system/payType.ajax?operFlg=4',
                data: {
                    payTypeID: this.payTypeID
                },
                call: 'online.edit.searchResult'
            });

        },

        searchResult: function( o ) {

            if (o.code == 200) {
                $('#partnerID').val(o.data.partnerID);
                $('#alias').val(o.data.alias);
                $('#seller_email').val(o.data.seller_email);
                $('#seller_name').val(o.data.seller_name);
                $('#service').val(o.data.service);
                $('#sign_type').val(o.data.sign_type);
                $('#md5_key').val(o.data.md5_key);
                $('#private_key').val(o.data.private_key);
                $('#public_key').val(o.data.public_key);
                $('#charset').val(o.data.charset);
                $('#gate_url').val(o.data.gate_url);
                $('#version').val(o.data.version);
                $('#extend_1').val(o.data.extend_1);
                $('#extend_2').val(o.data.extend_2);
                $('#extend_3').val(o.data.extend_3);
                $('#extend_4').val(o.data.extend_4);
                $('#extend_5').val(o.data.extend_5);
            }

        },

        open: function( payTypeID ) {

            this.payTypeID = payTypeID;

            E.popup.open({
                id: "pop3",
                title: '在线支付参数设置',
                css: "width: 600px;",
                btnName: "保存",
                btnCall: "online.edit.check();"
            });

            this.search();

        },

        check: function() {

            this.dt = E.getFormValues('online_form');
            var error_msg = '';

            if (E.isEmpty(this.dt.partnerID)) {
                error_msg += '请输入商户号<br>';
            }

            if (E.isEmpty(this.dt.alias)) {
                error_msg += '别名不能为空<br>';
            }

            if (E.isEmpty(this.dt.sign_type)) {
                error_msg += '签名方式不能为空';
            }

            if (error_msg != '') {
                E.alert( error_msg );
            } else {
                E.confirm('您确认要保存在线支付参数吗？', 'online.edit.execute')
            }

        },

        execute: function() {

            E.loadding.open('正在保存，请稍候...');

            this.dt.payTypeID = this.payTypeID;

            E.ajax_post({
                url: '/invokingAjax-backend/system/payType.ajax?operFlg=100&act=o_edit',
                data: this.dt,
                call: 'online.edit.result'
            });

        },

        result: function( o ) {

            E.loadding.close();
            if (o.code == 200) {
                E.alert(o.message, 2);
            } else {
                E.alert(o.message);
            }
            E.popup.close();
        }

    }

};

//绑定上传图片
KindEditor.ready(function(K) {

    var kindEditor_upload = K.editor({
        allowFileManager : true,
        uploadJson: '/framework/kindeditor/php/upload_json.php?act=system'
    });

    $(document).on('click', '#update_logo', function() {

        kindEditor_upload.loadPlugin('image', function() {
            kindEditor_upload.plugin.imageDialog({
                showRemote : false,
                clickFn : function(url) {

                    $('#logo_img_box').html('<img src="' + url + '"> <a href="javascript: void(0);" id="del_log_img">删除</a>').show();
                    $('#logo_img').val(url);
                    kindEditor_upload.hideDialog();

                }
            });
        });

    });

    $(document).on('click', '#del_log_img', function() {
        $('#logo_img_box').html('').hide();
        $('#logo_img').val('');
    });

});

//付款方式
var pays = {

    load: function(){
        $("#flex").flexReload();
    },

    edit: {

        open: function( id ) {


            E.popup.open({
                id: "pop1",
                title: id == 0 ? '新增付款方式' : '修改付款方式',
                css: "width: 800px;",
                btnName: "保存",
                btnCall: "pays.edit.check();"
            });

            if (id > 0) {

                E.ajax_get({
                    url: '/invokingAjax-backend/system/payType.ajax?operFlg=3',
                    data: {
                        id: id
                    },
                    call: function( o ) {
                        if (o.code == 200) {

                            $('#id').val(o.data.id);
                            $('#name').val(o.data.name);
                            $('#memo').val(o.data.memo);
                            if (o.data.logo_img) {
                                $('#logo_img_box').html('<img src="' + o.data.logo_img + '"> <a href="javascript: void(0);" id="del_log_img">删除</a>').show();
                            }

                            $('#type').val(o.data.type);
                            $('#is_invoice-' + o.data.is_invoice).iCheck('check');
                        } else {
                            E.popup.close();
                            E.alert(o.message);
                        }

                    }
                });

            }
            $('.square-radio').iCheck({
                checkboxClass: 'icheckbox_square-blue',
                radioClass: 'iradio_square-blue',
                increaseArea: '20%' // optional
            });

        },

        check: function() {

            this.dt = E.getFormValues('pay_add_form');
            var error_array = new Array();

            if (E.isEmpty(this.dt.name)) {
                error_array[0] = '付款方式名称不能为空';
            }
            if (this.dt.type == 0) {
                error_array[1] = '请选择付款方式类型';
            }

            if (error_array.length > 0) {
                E.alert(error_array.join('<br>'));
            } else {
                E.loadding.open('正在保存付款方式，请稍候...');
                this.dt.logo_img_box = $('#logo_img_box').html();
                E.ajax_post({
                    url: '/invokingAjax-backend/system/payType.ajax?operFlg=100&act=m_edit',
                    data: this.dt,
                    call: "pays.edit.result"
                });
            }

        },

        result: function( o ) {

            E.loadding.close();

            if (o.code == 200) {
                E.popup.close();
                E.alert(o.message, 2, 'pay.load');
            } else {
                E.alert(o.message);
            }

        }

    },

    del: {

        p_array: '',

        check: function() {

            var selected_count = $('.trSelected',flex).length;
            var tempArr = new Array();
            for(var i=0; i<selected_count; i++){
                var activityIDStr = $('.trSelected',flex).eq(i).attr('id');
                var activityIDStr = activityIDStr.substr(3);
                tempArr.push(activityIDStr);
            }

           // var p_obj = $('#flex').find('tr.trSelected');

            if (selected_count == 0) {
                E.alert('请选择要删除的付款方式');
            } else {
                this.p_array = tempArr;
                E.confirm('您确认要删除选中的付款方式吗？', 'pays.del.execute');

            }

        },

        execute: function() {

            //E.loadding.open('正在删除付款方式，请稍候...');
            E.ajax_post({
                url: '/invokingAjax-backend/system/payType.ajax?operFlg=100&act=m_del',
                data: {
                    p_array: this.p_array
                },
                call: 'pays.del.result'
            });
        },

        result: function( o ) {

            E.loadding.close();
            if (o.code == 200) {
                E.alert('删除成功', 2, 'pay.load');
            } else {
                E.alert(o.message);
            }

        }

    },

    change: {

        open: function( payTypeID, useFlg ) {

            this.payTypeID = payTypeID;
            this.useFlg = useFlg;

            if (useFlg == 1) {
                E.confirm('您确认要禁用该支付方式吗？', 'pay.change.execute')
            } else {
                E.confirm('您确认要启用该支付方式吗？', 'pay.change.execute')
            }

        },

        execute: function() {
            if (this.useFlg) {
                E.loadding.open('正在禁用支付方式，请稍候...');
            } else {
                E.loadding.open('正在启用支付方式，请稍候...');
            }
            E.ajax_get({
                url: '/invokingAjax-backend/system/payType.ajax?operFlg=100&act=change',
                data: {
                    payTypeID: this.payTypeID
                },
                call: 'pay.change.result'
            });
        },

        result: function( o ) {

            E.loadding.close();
            if (o.code == 200) {
                E.alert(o.message, 2, 'pay.load');
            } else {
                E.alert(o.message);
            }

        }

    }

};
